package com.zhangdi.demo.es.controller.rest;

import com.zhangdi.demo.es.entity.CompanyDoc;
import com.zhangdi.demo.es.entity.EngineerDoc;
import com.zhangdi.demo.es.mapper.CompanyEngineerMapper;
import com.zhangdi.demo.es.response.Response;
import com.zhangdi.demo.es.response.ResponseUtils;
import com.zhangdi.demo.es.service.ESUserService;
import com.zhangdi.demo.es.service.TestDoc;
import com.zhangdi.demo.es.service.UserService;
import com.zhangdi.demo.es.thread.ThreadBoss;
import com.zhangdi.mserver.searchcli.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangdi on 2020/06/24 15:04:50.
 */

@RestController
public class TestController {
    Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    UserService userService;
    @Autowired
    ESUserService esUserService;

    @Autowired
    ThreadBoss threadBoss;

//    @GetMapping("/es/search")
//    public List<CompanyDoc> query(String str) {
//        List<CompanyDoc> companies = new ArrayList<>();
//
////        companies = userService.list();
//
//        return companies;
//    }

//    @PostMapping("/mg/add")
//    public Response<CompanyDoc> addUser(@RequestBody Company company) {
//        return userService.add(company);
//    }
//
//    @PostMapping("/mg/list")
//    public Response<List<Company>> list() {
//        return ResponseUtils.success(userService.list());
//    }
//
//    @PostMapping("/mg/findbyids")
//    public Response<List<Company>> findByIds() {
//        return ResponseUtils.success(userService.findById(null));
//    }
//
//    @PostMapping("/es/list")
//    public Response<List<Company>> esfindAll() {
//        return ResponseUtils.success(esUserService.findAll());
//    }

    @PostMapping("/es/search")
    public Response<Page<CompanyDoc>> search(@RequestBody Map<String, Object> params) {
        return ResponseUtils.success(esUserService.findByKeyValue(params));
    }

    @GetMapping("/er/id")
    public EngineerDoc findById(long id){
        return testDoc.findById(id);
    }

    @Resource
    ElasticsearchRestTemplate template;

    @PostMapping("/es/input")
    public Response input(String index) {
        if (!template.indexExists(index)) {
            //读取mapping 转为json 做put请求
        }
        threadBoss.run();
        return ResponseUtils.success();
    }

    @Resource
    TestDoc testDoc;

    @PostMapping("/es/insert")
    public String testSave(@RequestBody EngineerDoc engineer){
        return testDoc.insert(engineer);
    }

    @PostMapping("/es/update")
    public String testUpdate(@RequestBody EngineerDoc engineer){
        return testDoc.update(engineer);
    }

    @PostMapping("/es/insertcom")
    public CompanyDoc testSaveCompany(@RequestBody CompanyDoc doc){
        return testDoc.insertCompany(doc);
    }

    @GetMapping("/es/initcomdocs")
    public String initCompanyDocs(){
        CompanyDoc doc1 = new CompanyDoc();
        doc1.setId(1);
        doc1.setName("营口中北工程有限公司");
        doc1.setSname(doc1.getName());
        testDoc.insertCompany(doc1);

        CompanyDoc doc2 = new CompanyDoc();
        doc2.setId(2);
        doc2.setName("中北工程设计咨询有限公司");
        doc2.setSname(doc2.getName());
        testDoc.insertCompany(doc2);

        CompanyDoc doc3 = new CompanyDoc();
        doc3.setId(3);
        doc3.setName("南昌中北工程设计咨询有限公司");
        doc3.setSname(doc3.getName());
        testDoc.insertCompany(doc3);

        CompanyDoc doc4 = new CompanyDoc();
        doc4.setId(4);
        doc4.setName("陕西华通公路工程公司");
        doc4.setSname(doc4.getName());
        testDoc.insertCompany(doc4);

        CompanyDoc doc5 = new CompanyDoc();
        doc5.setId(5);
        doc5.setName("陕西公路工程建设有限公司");
        doc5.setSname(doc5.getName());
        testDoc.insertCompany(doc5);

        CompanyDoc doc6 = new CompanyDoc();
        doc6.setId(6);
        doc6.setName("陕西现代公路机械工程有限公司");
        doc6.setSname(doc6.getName());
        testDoc.insertCompany(doc6);

        CompanyDoc doc7 = new CompanyDoc();
        doc7.setId(7);
        doc7.setName("陕西公路交通集团");
        doc7.setSname(doc7.getName());
        testDoc.insertCompany(doc7);

        return "success";
    }

    @Resource
    CompanyEngineerMapper engineerMapper;

    @GetMapping("testlong")
    public long testLong(){
        Long id = engineerMapper.findCompanyIdById(1);

        return id == null?0:id;
    }

    @GetMapping("/es/init2")
    public String init2(){
        CompanyDoc doc1 = new CompanyDoc();
//        doc1.setId(1);
        doc1.setName("营口中北工程有限公司");
        doc1.setSname(doc1.getName());
        List<Integer> records = new ArrayList<>();
        records.add(111);
        records.add(222);
        records.add(333);
        doc1.setRecords(records);
        doc1.setAreaId(8);
        testDoc.insertCompany(doc1);

        CompanyDoc doc2 = new CompanyDoc();
//        doc2.setId(2);
        doc2.setName("中北工程设计咨询有限公司");
        doc2.setSname(doc2.getName());
        List<Integer> records2 = new ArrayList<>();
        records2.add(333);
        records2.add(444);
        records2.add(555);
        doc2.setRecords(records2);
        doc2.setAreaId(8);
        testDoc.insertCompany(doc2);

        CompanyDoc doc3 = new CompanyDoc();
        doc3.setId(3);
        doc3.setName("南昌中北工程设计咨询有限公司");
        doc3.setSname(doc3.getName());
        List<Integer> records3 = new ArrayList<>();
        records3.add(111);
        records3.add(666);
        records3.add(333);
        doc3.setRecords(records3);
        doc3.setAreaId(26);
        testDoc.insertCompany(doc3);

        return "success";
    }
}
